/**	
 * Author : Mht
 * Desc : For 2016 RDM 
 */
package bean;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.quartz.SchedulerException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import common.DynamicSetting;
import entity.schedule.ScheduleJob;
import lombok.extern.log4j.Log4j;
import schedule.ScheduleService;
import schedule.impl.JobTest;
import service.IHSService;

/**
 * @author Mht
 * @date Jun 3, 2016
 */
@Log4j
@RestController
public class ScheduleController extends BaseController{

	@Resource
	private ScheduleService scheduleService;
	
	@Resource
	private IHSService ihsService;
	
	@Resource
	private DynamicSetting setting;

	@RequestMapping("/addJob")
	@ResponseBody
	public void addJob(String jobName, String jobGroup, String cronExpression, String description, String email) {
		ScheduleJob job = new ScheduleJob();
//		ihsService.queryLocalList();
//		ihsService.queryMosaicList();
		job.setJobId(UUID.randomUUID().toString());
		job.setJobName(jobName);
		job.setJobGroup(jobGroup);
		job.setJobStatus("1");
		// job.setCronExpression("0/5 * * * * ?");
		job.setCronExpression(cronExpression);
		job.setEmail(email);
		job.setDescription(description);
		job.setJobKey("scheduleJob");
		job.setClazz(JobTest.class);
		scheduleService.addOrEditJob(job);
		log.info("新增加作业:" + job.getJobName());
	}

	@RequestMapping("/editJob")
	@ResponseBody
	public void editJob(String jobName, String jobGroup, String cronExpression, String description, String email) {
		ScheduleJob job = new ScheduleJob();
		job.setJobId(UUID.randomUUID().toString());
		job.setJobName(jobName);
		job.setJobGroup(jobGroup);
		job.setJobStatus("1");
		// job.setCronExpression("0/5 * * * * ?");
		job.setCronExpression(cronExpression);
		job.setEmail(email);
		job.setDescription(description);
		job.setJobKey("scheduleJob");
		job.setClazz(JobTest.class);
		scheduleService.addOrEditJob(job);
		log.info("更新作业:" + job.getJobName());
	}

	@RequestMapping("/pauseJob")
	@ResponseBody
	public void pauseJob(String jobName) {
		scheduleService.pauseJob(jobName);
	}

	@RequestMapping("/deleteJob")
	@ResponseBody
	public void deleteJob(String jobName) {
		scheduleService.deleteJob(jobName);
	}

	@RequestMapping("/resumeJob")
	@ResponseBody
	public void resumeJob(String jobName) {
		scheduleService.resumeJob(jobName);
	}

	@RequestMapping("/shutJob")
	@ResponseBody
	public void shutSchedule() {
		scheduleService.shutdownSchedule();
	}

	@RequestMapping(value = "/getAllJob")
	@ResponseBody
	public List<ScheduleJob> getAllJob() {
		try {
			return scheduleService.getAllJob();
		} catch (SchedulerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new ArrayList<ScheduleJob>();
		}
	}
}
